package com.fr.schedule.trigger;

import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.DateUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.schedule.dao.FieldMapper;
import com.fr.schedule.dao.ObjectMapper;
import com.fr.schedule.dao.ValueFieldMapper;
import com.fr.third.org.quartz.SimpleTrigger;
import com.fr.third.org.quartz.Trigger;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.TimeZone;
import oracle.sql.TIMESTAMP;

public class SimpleITrigger
  implements ITrigger
{
  public static final String TABLE_NAME = "fr_simple_itrigger";
  public static final int IMMEDIATE = 1;
  public static final int ON = 2;
  public static final int MINUTES = 1;
  public static final int HOURS = 2;
  public static final int DAYS = 3;
  public static final int WEEKS = 4;
  private long id = -1L;
  private int version;
  private String timeZoneID = TimeZone.getDefault().getID();
  private long startType = 1L;
  private Date startTime = null;
  private long repeatCount = -1L;
  private Date endTime = null;
  private long recurrenceInterval = 1L;
  private long recurrenceIntervalUnit = 3L;
  public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper("fr_simple_itrigger", new FieldMapper[] { new FieldMapper("id", 4, "id"), new FieldMapper("timeZoneID", 12, "timezone_id"), new FieldMapper("startType", 4, "start_type"), new ValueFieldMapper("startTime", 93, "startTime")
  {
    public Object field2Value(Object paramObject)
    {
      return paramObject;
    }

    public Object value2Field(Object paramObject)
    {
      if (paramObject instanceof Timestamp)
        return new Date(((Timestamp)paramObject).getTime());
      if (paramObject instanceof TIMESTAMP)
        try
        {
          return new Date(((TIMESTAMP)paramObject).timestampValue().getTime());
        }
        catch (SQLException localSQLException)
        {
          localSQLException.printStackTrace();
        }
      return paramObject;
    }
  }
  , new FieldMapper("repeatCount", 4, "repeat_count"), new ValueFieldMapper("endTime", 93, "endTime")
  {
    public Object field2Value(Object paramObject)
    {
      return paramObject;
    }

    public Object value2Field(Object paramObject)
    {
      if (paramObject instanceof Timestamp)
        return new Date(((Timestamp)paramObject).getTime());
      if (paramObject instanceof TIMESTAMP)
        try
        {
          return new Date(((TIMESTAMP)paramObject).timestampValue().getTime());
        }
        catch (SQLException localSQLException)
        {
          localSQLException.printStackTrace();
        }
      return paramObject;
    }
  }
  , new FieldMapper("recurrenceInterval", 4, "recurrence_interval"), new FieldMapper("recurrenceIntervalUnit", 4, "recurrence_interval_unit") });

  public static SimpleITrigger analyzeJSON(JSONObject paramJSONObject)
  {
    SimpleITrigger localSimpleITrigger;
    try
    {
      localSimpleITrigger = new SimpleITrigger();
      long l1 = paramJSONObject.getLong("startType");
      localSimpleITrigger.setStartType(l1);
      if (l1 == 2L)
      {
        String str1 = paramJSONObject.getString("startTime");
        try
        {
          Date localDate1 = DateUtils.DATETIMEFORMAT2.parse(str1);
          localSimpleITrigger.setStartTime(localDate1);
        }
        catch (ParseException localParseException1)
        {
          localParseException1.printStackTrace();
        }
      }
      if (paramJSONObject.has("repeatCount"))
      {
        long l2 = paramJSONObject.getLong("repeatCount");
        localSimpleITrigger.setRepeatCount(l2);
      }
      if (paramJSONObject.has("endTime"))
      {
        String str2 = paramJSONObject.getString("endTime");
        try
        {
          Date localDate2 = DateUtils.DATETIMEFORMAT2.parse(str2);
          localSimpleITrigger.setEndTime(localDate2);
        }
        catch (ParseException localParseException2)
        {
          localParseException2.printStackTrace();
        }
      }
      long l3 = paramJSONObject.getLong("recurrenceInterval");
      localSimpleITrigger.setRecurrenceInterval(l3);
      long l4 = paramJSONObject.getLong("recurrenceIntervalUnit");
      localSimpleITrigger.setRecurrenceIntervalUnit(l4);
      if (paramJSONObject.has("id"))
        localSimpleITrigger.setId(paramJSONObject.getLong("id"));
      return localSimpleITrigger;
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    return null;
  }

  public JSONObject createJSONConfig()
  {
    JSONObject localJSONObject = new JSONObject();
    try
    {
      Date localDate;
      String str;
      localJSONObject.put("id", getId());
      localJSONObject.put("startType", getStartType());
      if (2L == getStartType())
      {
        localDate = getStartTime();
        str = DateUtils.DATETIMEFORMAT2.format(localDate);
        localJSONObject.put("startTime", str);
      }
      localJSONObject.put("recurrenceType", "simple");
      if (getEndTime() == null)
      {
        localJSONObject.put("repeatCount", getRepeatCount());
      }
      else
      {
        localDate = getEndTime();
        str = DateUtils.DATETIMEFORMAT2.format(localDate);
        localJSONObject.put("endTime", str);
      }
      localJSONObject.put("recurrenceInterval", getRecurrenceInterval());
      localJSONObject.put("recurrenceIntervalUnit", getRecurrenceIntervalUnit());
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    return localJSONObject;
  }

  public Trigger createTrigger()
  {
    SimpleTrigger localSimpleTrigger = new SimpleTrigger("trigger_" + getId() + "_" + getITriggerIdPrefix(), "ReportJobs");
    if (getStartType() == 1L)
      break label95:
    if ((getStartType() == 2L) && (getStartTime() != null))
      localSimpleTrigger.setStartTime(getStartTime());
    else
      throw new IllegalStateException("The statemenet is illegal.");
    if (getRepeatCount() > -2L)
      label95: localSimpleTrigger.setRepeatCount((int)getRepeatCount());
    if (getEndTime() != null)
      localSimpleTrigger.setEndTime(getEndTime());
    long l = 0L;
    switch ((int)getRecurrenceIntervalUnit())
    {
    case 1:
      l = getRecurrenceInterval() * 60L * 1000L;
      break;
    case 2:
      l = getRecurrenceInterval() * 60L * 60L * 1000L;
      break;
    case 3:
      l = getRecurrenceInterval() * 24L * 60L * 60L * 1000L;
      break;
    case 4:
      l = getRecurrenceInterval() * 7L * 24L * 60L * 60L * 1000L;
    }
    localSimpleTrigger.setRepeatInterval(l);
    return localSimpleTrigger;
  }

  public int getITriggerIdPrefix()
  {
    return 2;
  }

  public void setId(long paramLong)
  {
    this.id = paramLong;
  }

  public long getId()
  {
    return this.id;
  }

  public void setVersion(int paramInt)
  {
    this.version = paramInt;
  }

  public int getVersion()
  {
    return this.version;
  }

  public void setTimeZoneID(String paramString)
  {
    this.timeZoneID = paramString;
  }

  public String getTimeZoneID()
  {
    return this.timeZoneID;
  }

  public void setStartType(long paramLong)
  {
    this.startType = paramLong;
  }

  public long getStartType()
  {
    return this.startType;
  }

  public void setStartTime(Date paramDate)
  {
    this.startTime = paramDate;
  }

  public Date getStartTime()
  {
    return this.startTime;
  }

  public void setRepeatCount(long paramLong)
  {
    this.repeatCount = paramLong;
  }

  public long getRepeatCount()
  {
    return this.repeatCount;
  }

  public void setEndTime(Date paramDate)
  {
    this.endTime = paramDate;
  }

  public Date getEndTime()
  {
    return this.endTime;
  }

  public void setRecurrenceInterval(long paramLong)
  {
    this.recurrenceInterval = paramLong;
  }

  public long getRecurrenceInterval()
  {
    return this.recurrenceInterval;
  }

  public void setRecurrenceIntervalUnit(long paramLong)
  {
    this.recurrenceIntervalUnit = paramLong;
  }

  public long getRecurrenceIntervalUnit()
  {
    return this.recurrenceIntervalUnit;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof SimpleITrigger))
      return false;
    SimpleITrigger localSimpleITrigger = (SimpleITrigger)paramObject;
    return ((getId() == localSimpleITrigger.getId()) && (getVersion() == localSimpleITrigger.getVersion()) && (getStartType() == localSimpleITrigger.getStartType()) && (ComparatorUtils.equals(getStartTime(), localSimpleITrigger.getStartTime())) && (getRepeatCount() == localSimpleITrigger.getRepeatCount()) && (ComparatorUtils.equals(getEndTime(), localSimpleITrigger.getEndTime())) && (getRecurrenceInterval() == localSimpleITrigger.getRecurrenceInterval()) && (getRecurrenceIntervalUnit() == localSimpleITrigger.getRecurrenceIntervalUnit()));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    SimpleITrigger localSimpleITrigger = (SimpleITrigger)super.clone();
    return localSimpleITrigger;
  }
}